package org.acooly.secretbox.ui;

import java.util.ArrayList;
import java.util.List;

import org.acooly.android.utils.StringUtils;
import org.acooly.android.utils.ToastUtils;
import org.acooly.secretbox.R;
import org.acooly.secretbox.common.BaseActivity;
import org.acooly.secretbox.service.UserService;
import org.acooly.secretbox.service.domain.User;
import org.acooly.secretbox.service.impl.UserServiceImpl;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

/**
 * 忘记密码
 * 
 * @author zhangpu
 * 
 */
public class UserManageActivity extends BaseActivity {

	private UserService userService;
	Spinner forgetQuestion;
	List<String> listQuestions = null;
	User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_manage);
		userService = new UserServiceImpl(this);
		user = userService.get();

		forgetQuestion = (Spinner) findViewById(R.id.user_forgetQuesture);
		// 获取相应对象
		String[] ls = getResources().getStringArray(R.array.questions);
		listQuestions = new ArrayList<String>();
		// 获取XML中定义的数组
		for (int i = 0; i < ls.length; i++) {
			listQuestions.add(ls[i]);
		}
		ArrayAdapter<String> forgetQuestionType = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listQuestions);
		forgetQuestionType.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
		forgetQuestion.setAdapter(forgetQuestionType);
		forgetQuestion.setSelection(forgetQuestionType.getPosition(user.getQuestion()));
		setEditText(R.id.user_forgetAnswer, user.getAnswer());

		Button saveButton = (Button) getView(R.id.user_manage_save);
		saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				save();
			}
		});

	}

	private void save() {
		try {
			String curPassword = getTextView(R.id.user_password);
			if (!StringUtils.equals(curPassword, user.getPassword())) {
				throw new RuntimeException("密码错误，请重试.");
			}
			String password = ((EditText) findViewById(R.id.user_pswd)).getText().toString();
			String confirmPassword = ((EditText) findViewById(R.id.user_confirmPswd)).getText().toString();
			Spinner spinner = (Spinner) findViewById(R.id.user_forgetQuesture);
			String question = listQuestions.get((int) spinner.getSelectedItemId());
			String answer = ((EditText) findViewById(R.id.user_forgetAnswer)).getText().toString();
			if (StringUtils.isBlank(password)) {
				throw new RuntimeException("登录密码不能为空");
			}
			if (StringUtils.isBlank(confirmPassword)) {
				throw new RuntimeException("确认密码不能为空");
			}

			if (!password.equals(confirmPassword)) {
				throw new RuntimeException("两次输入的密码不相同");
			}

			if (StringUtils.isBlank(answer)) {
				throw new RuntimeException("问题答案不能为空");
			}
			user.setAnswer(answer);
			user.setPassword(password);
			user.setQuestion(question);
			userService.update(user);
			finish();
		} catch (Exception e) {
			ToastUtils.showShort(this, e.getMessage());
		}
	}

}
